const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 秒杀
const seckill = (rest) => {
  let days = formatNumber(parseInt(rest / 60 / 60 / 24)),
    hours = formatNumber(parseInt(rest / 60 / 60 % 24)),
    minutes = formatNumber(parseInt(rest / 60 % 60)),
    seconds = formatNumber(parseInt(rest % 60));
  return { d: days, h: hours, m: minutes, s: seconds }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  seckill: seckill
}
